package com.company.project.redis;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.ValueOperations;

import javax.annotation.PostConstruct;
import java.util.concurrent.TimeUnit;

/**
 *
 *
 * @author chenx
 * @date 2016/6/18
 */
public class CaptchaRedis {
    private final String KEY = "captcha";
    //超时时间
    private static int captchaExpires = 3 * 60; //超时时间3min

    @Autowired
    private StringRedisTemplate redisTemplate;

    private ValueOperations<String, String> valueOperations;

    @PostConstruct
    public void init() {
        valueOperations = redisTemplate.opsForValue();
    }

    /**
     * 设置内容
     *
     * @author chenx
     * @date 2016/6/18
     */
    public void setModelValue(String id, String value) {
        if (StringUtils.isNotBlank(value)) {
            valueOperations.set(KeyBuilder.generateKey(KEY, String.valueOf(id)), value,captchaExpires, TimeUnit.SECONDS);
        }
    }

    /**
     * 获取指定酒店的时区
     *
     * @param id 查询的编号
     * @return 时区
     *
     * @author chenx
     * @date 2016/6/18
     */
    public String getModelValue(String id) {
        return valueOperations.get(KeyBuilder.generateKey(KEY,String.valueOf(id)));
    }
}
